import { AppDataSource } from "./data-source"
import webService from "./services/web_service";
import { PORT, HOST } from "./utils/address-utils";
import "reflect-metadata";
import { videoProjectEntity } from './entity/videoProject_entity'
import { NodeEntity } from './entity/node_entity'
import { VideoEntity } from './entity/video_entity'
import { fixURL, pathToURL, urlToPath } from "./utils/address-utils";


//官方文档 https://typeorm.bootcss.com/
AppDataSource.initialize().then(async () => {

    const projectRepository = AppDataSource.getRepository(videoProjectEntity)
    const nodeRepository = AppDataSource.getRepository(NodeEntity)
    const videoRepository = AppDataSource.getRepository(VideoEntity)
    let projectList: videoProjectEntity[] = await projectRepository.find();
    let nodeList: NodeEntity[] = await nodeRepository.find();
    let videoList: VideoEntity[] = await videoRepository.find();

    for (let index = 0; index < projectList.length; index++) {
        const element = projectList[index];
        element.cover = fixURL(element.cover.toString());
        await projectRepository.save(element)
    }

    for (let index = 0; index < nodeList.length; index++) {
        const element = nodeList[index];
        element.thumbnail = fixURL(element.thumbnail.toString());
        element.videoUrl = fixURL(element.videoUrl.toString());
        await nodeRepository.save(element)
    }

    for (let index = 0; index < videoList.length; index++) {
        const element = videoList[index];
        element.thumbnail = fixURL(element.thumbnail.toString());
        element.videoUrl = fixURL(element.videoUrl.toString());
        await videoRepository.save(element)
    }

    console.log('db fix complete!!');

}).catch(error => console.log(error))
